#define DEBUG
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn=500, inf=0x7ffffff;

int min(int a, int b) {
	return a<b ? a : b;
}

class Net {
	public:
		static const int maxm=(maxn+2)*(maxn+2), s=1, t=2;
		bool flag;
		int n, m, ind[maxn+3], to[2*maxm+1], link[2*maxm+1], val[2*maxm+1], cur[maxn+3], dis[maxn+3];

		void init(int n) {
			this->n = n, m=1;
			flag = false;
		}

		void addSide(int u, int v, int w) {
			m++;
			to[m] = v;
			val[m] = w;
			link[m] = ind[u];
			ind[u] = m;
		}

		void addTwoSide(int u, int v, int w) {
			addSide(u, v, w);
			addSide(v, u, 0);
		}

		void spfa() {
			static int que[maxn+1];
			static bool book[maxn+1];

			memset(dis, 127, sizeof dis);
			dis[s] = 0, que[1] = s, book[s] = true;
			for (int h=0, t=1; h!=t; ) {
				h=h%n+1;
				int x=que[h], y;
				book[x] = false;
				for (int i=ind[x]; i; i=link[i]) {
					y = to[i];
					if (val[i]) {
						if (dis[y]>dis[x]+1) {
							dis[y] = dis[x]+1;
							if (!book[y]) {
								book[y] = true;
								t = t%n+1;
								que[t] = y;
							}
						}
					}
				}
			}
		}

		int run(int o, int flow) {
			if (o==t) {
				return flow;
			}
			int have=0;
			for (int i=cur[o]; i; i=link[i]) {
				cur[o] = i;
				if (dis[to[i]]==dis[o]+1) {
					int t=run(to[i], min(flow-have, val[i]));
					val[i]-=t, val[i^1]+=t, have+=t;
					if (have==flow) {
						break;
					}
				}
			}
			return have;
		}

		int solve() {
			int ret=0;
			for (spfa(); dis[t]<=n; spfa()) {
				for (int i=1; i<=n; i++) {
					cur[i] = ind[i];
				}
				ret += run(s, inf);
			}
			return ret;
		}
};

int main() {
	freopen("algebra.in", "r", stdin);
	freopen("algebra.out", "w", stdout);

	static Net g;
	static int b[maxn+1][maxn+1], c[maxn+1];
	int n, sum=0;
	scanf("%d", &n);
	g.init(n+2);
	for (int i=1; i<=n; i++) {
		int now=0;
		for (int j=1; j<=n; j++) {
			scanf("%d", &b[i][j]);
			g.addTwoSide(j+2, i+2, b[i][j]);
			now += b[i][j];
		}
		g.addTwoSide(g.s, i+2, now);
		sum += now;
	}
	for (int i=1; i<=n; i++) {
		scanf("%d", c+i);
		g.addTwoSide(i+2, g.t, c[i]);
	}

	printf("%d", sum-g.solve());

	fclose(stdin);
	fclose(stdout);
	return 0;
}
